package com.touch.jgloo.core;
import java.awt.*;
import javax.media.opengl.*;
import com.sun.opengl.util.texture.*;
/**
 * The JGLTriad class is basically an Object drawn in the shape of a Triangle.
 * @author Homicidal Monkey
 * @version 10.1.9
 * @since r419
 * @see com/touch/Licence
 */
public class JGLTriad extends JGLObject 
{
	/**
	 * Creates a JGLTriad using the size of 1.
	 * @since r461
	 */
	public JGLTriad()
	{
		//specify the points
		final float verts[] = {-1f, 1f, 0f, 1f, -1f, 0f, -1f, -1f ,0f};
		
		//add the points to the JGLObject
		this.addPolygon(new JGLPolygon(3));
		//sets the triad
		this.setTriad(verts);
	}
	/**
	 * Creates a JGLTriad using a specified size
	 * @param size size of the JGLTriad. Note that the size is 1/2 the size of any line constructing the Triangle.
	 * @since r461
	 */
	public JGLTriad(final float size)
	{
		//specify the points
		final float verts[] = {-size, size, 0, size, -size, 0, -size, -size,0};
		
		//add the polygon to the Object
		this.addPolygon(new JGLPolygon(3));
		//sets the Triad.
		this.setTriad(verts);
	}	
	/** 
	 * Sets the JGLTriad using pre-defined points.
	 * @param verts Points, there must be exactly 9 points. 
	 * @since r461 
	 */
	public final synchronized void setTriad(final float ...verts)
	{
		//Take the polygon object
		final JGLPolygon polygon = this.getPolygon();
		
		//set the polygon's render state to Traingle
		polygon.renderState = GL.GL_TRIANGLES;
		
		//set the points of the Triangle
		polygon.setPoint(0, new JGLPoint(verts[0], verts[1], verts[2]));
		polygon.setPoint(1, new JGLPoint(verts[3], verts[4], verts[5]));
		polygon.setPoint(2, new JGLPoint(verts[6], verts[7], verts[8]));
	}
	/**
	 * Returns the Polygon object representing the JGLTriad.
	 * @return Polygon that defines the JGLTriad.
	 * @since r461
	 */
	public JGLPolygon getPolygon()
	{
		//return the root polygon (first polygon).
		return this.getPolygon(0);
	}	
	/**
	 * Sets the color of the JGLTriad
	 * @param color Color to set the JGLTriad
	 * @since r461
	 */
	public void setColor(final Color color)
	{
		//get the root polygon
		final JGLPolygon polygon = this.getPolygon();
		
		//set the color of each point.
		for(int i =0; i < 3; i++)
			polygon.setColor(i, color);
	}
	/**
	 * Sets the color of the JGLTriad. Note: there must be at least three colors.
	 * @param color A list of colors to set the points by.
	 * @since r461
	 */
	public void setColor(final Color ...color)
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon();
		
		//set the color of each point.
		for(int i = 0; i < 3; i++)
			polygon.setColor(i, color[i]);
	}
	/**
	 * Sets the Texture of the JGLTriad
	 * @param texture Texture to use
	 * @since r461
	 */
	public void setTexture(final Texture texture)
	{
		//texture the root polygon.
		this.setTexture(0, texture);
	}
	/** 
	 * Overrides the "String toString" method of Object and returns
	 * information about the JGLTriad.
	 * @return Description of the JGLTriad.
	 * @since r461
	 */
	public String toString()
	{
		//get the polygon
		final JGLPolygon polygon = this.getPolygon();
		//return the info of each point.
		return "JGLTriad: " + 
			polygon.getPoint(0).toString() + ", " + 
			polygon.getPoint(1).toString() + ", " + 
			polygon.getPoint(2).toString();
	}
}

